package com.ruoyi.web.controller.test;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.TencentCosService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 腾讯云COS测试控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/test/cos")
public class CosTestController {
    
    private static final Logger logger = LoggerFactory.getLogger(CosTestController.class);
    
    @Autowired
    private TencentCosService tencentCosService;
    
    /**
     * 测试文件上传到腾讯云COS
     */
    @PostMapping("/upload")
    public AjaxResult testUpload(@RequestParam("file") MultipartFile file) {
        try {
            if (file == null || file.isEmpty()) {
                return AjaxResult.error("上传文件不能为空");
            }
            
            logger.info("开始测试上传文件: {}, 大小: {} bytes", file.getOriginalFilename(), file.getSize());
            
            // 上传文件到腾讯云COS
            String fileUrl = tencentCosService.uploadFile(file);
            
            Map<String, Object> result = new HashMap<>();
            result.put("originalFilename", file.getOriginalFilename());
            result.put("fileSize", file.getSize());
            result.put("contentType", file.getContentType());
            result.put("fileUrl", fileUrl);
            result.put("fileName", fileUrl.substring(fileUrl.lastIndexOf("/") + 1));
            
            logger.info("文件上传成功: {}", fileUrl);
            
            return AjaxResult.success("文件上传成功", result);
            
        } catch (Exception e) {
            logger.error("文件上传失败: {}", e.getMessage(), e);
            return AjaxResult.error("文件上传失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试腾讯云COS配置
     */
    @GetMapping("/config")
    public AjaxResult testConfig() {
        try {
            Map<String, Object> config = new HashMap<>();
            config.put("message", "腾讯云COS配置测试接口");
            config.put("status", "ready");
            config.put("timestamp", System.currentTimeMillis());
            
            return AjaxResult.success("配置测试成功", config);
            
        } catch (Exception e) {
            logger.error("配置测试失败: {}", e.getMessage(), e);
            return AjaxResult.error("配置测试失败: " + e.getMessage());
        }
    }
}